package com.first.nell.components.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.databinding.DialogFirstNellCreateVirtualAccountBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*

class NgnCreateVirtualAccountDialog : DialogFragment() {

    companion object {
        fun showFirstNellDialog(
            fragmentManager: FragmentManager,
            scope: CoroutineScope,
            onFinish: () -> Unit
        ) {
            val dialog = NgnCreateVirtualAccountDialog()
            dialog.mFirstNellScope = scope
            dialog.mFirstNellOnFirstNellFinishFirstNellListener = onFinish
            dialog.showNow(
                fragmentManager,
                NgnCreateVirtualAccountDialog::class.java.simpleName
            )
        }
    }

    lateinit var mFirstNellViewFirstNellBinding: DialogFirstNellCreateVirtualAccountBinding

    private var mFirstNellCutdownFirstNellFlow: Job? = null
    private var mFirstNellScope: CoroutineScope? = null
    private var mFirstNellOnFirstNellFinishFirstNellListener: () -> Unit = {}


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mFirstNellViewFirstNellBinding = DialogFirstNellCreateVirtualAccountBinding.inflate(layoutInflater)

        return mFirstNellViewFirstNellBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        mFirstNellScope?.let { startFirstNellCutdown(it) }
    }

    override fun onDestroy() {
        mFirstNellCutdownFirstNellFlow?.cancel()
        super.onDestroy()
    }

    private fun initFirstNellCutdownFirstNellFlow(
        scope: CoroutineScope,
        onTick: (Int) -> Unit,
        onFinish: () -> Unit,
    ): Job {
        return flow {
            for (tickSec in 10 downTo 0) {
                emit(tickSec)
                if (tickSec != 0) {
                    delay(1000)
                }
            }
        }.flowOn(Dispatchers.Main)
            .onEach { onTick.invoke(it) }
            .onCompletion { onFinish.invoke() }
            .launchIn(scope)
    }

    @SuppressLint("SetTextI18n")
    fun startFirstNellCutdown(scope: CoroutineScope) {
        mFirstNellCutdownFirstNellFlow = initFirstNellCutdownFirstNellFlow(scope,
            {
                if (it == 0) {
                    mFirstNellCutdownFirstNellFlow?.cancel()
                    mFirstNellOnFirstNellFinishFirstNellListener.invoke()
                    dismiss()
                } else {
                    mFirstNellViewFirstNellBinding.textDialogCreateVirtualAccountCutdown.text = "${it}s"
                }
            },
            {})
    }

    fun stopFirstNellCutdown() {
        mFirstNellCutdownFirstNellFlow?.cancel()
    }
}